<?php
$this->pageTitle = 'Comments - Administrator';
?>

<script>
    $(document).ready(function(){
        $('.comment-block-title a.approve').click(function(){
            var input_comment = $(this).attr('id').split('-')[1];
            if(input_comment.length>0){
                 $.ajax({
                        type: "POST",
                        data: ({comment: input_comment}),
                        dataType: "json",
                        url: "<?=$baseUrl?>/comments/approve",

                        success: function(data){
                            if(data.result){
                                $('#comment-menu-'+input_comment).remove();
                                $('#comment-status-'+input_comment).append('approved!').fadeIn('slow');
                            }else if(data.error){
                                if(data.error == '0'){ alert('No comment selected');}
                                else {
                                    alert('Failed when saving. Please try again');
                                }
                            }else{
                                alert('Please check your connection').show();
                            }
                        },
                        error:function(XMLHttpRequest,textStatus, errorThrown)
                        {
                            var status = XMLHttpRequest.status;
                            if(status == '403'){
                                alert('Please check your connection').show();
                            }
                        }
                    });
            }else{
                alert('No comment selected');
            }
            return false;
        });

        $('.comment-block-title a.delete').click(function(){
            var input_comment = $(this).attr('id').split('-')[1];
            if(input_comment.length>0){
                 $.ajax({
                        type: "POST",
                        data: ({comment: input_comment}),
                        dataType: "json",
                        url: "<?=$baseUrl?>/comments/delete",

                        success: function(data){
                            if(data.result){
                                $('#comment-block-'+input_comment).fadeOut('slow',function(){
                                    $(this).remove();
                                });
                            }else if(data.error){
                                alert('No comment selected');
                            }else{
                                alert('Please check your connection').show();
                            }
                        },
                        error:function(XMLHttpRequest,textStatus, errorThrown)
                        {
                            var status = XMLHttpRequest.status;
                            if(status == '403'){
                                alert('Please check your connection').show();
                            }
                        }
                    });
            }else{
                alert('No comment selected');
            }
            return false;
        });

        $('.comment-block-title a.permission').click(function(){
            var id = $(this).attr('id').split('-')[1];
            var input_email = $('#email-'+id).text();
            if(input_email.length>0){
                 $.ajax({
                        type: "POST",
                        data: ({email: input_email}),
                        dataType: "json",
                        url: "<?=$baseUrl?>/visitors/approve",

                        success: function(data){
                            if(data.result){
                                $('#status-permission-'+id).remove();
                                $('#comment-menu-'+id).remove();
                                $('#permission-user-'+id).empty().append('Succesfully give permission');
                                $('#permission-result-'+id).empty().append('Succesfully give permission');
                            }else if(data.error){
                                alert('No comment selected');
                            }else{
                                alert('Please check your connection').show();
                            }
                        },
                        error:function(XMLHttpRequest,textStatus, errorThrown)
                        {
                            var status = XMLHttpRequest.status;
                            if(status == '403'){
                                alert('Please check your connection').show();
                            }
                        }
                    });
            }else{
                alert('No comment selected');
            }
            return false;
        });

        $('.comment-block-title a.deny').click(function(){
            var id = $(this).attr('id').split('-')[1];
            var input_email = $('#email-'+id).text();
            if(input_email.length>0){
                 $.ajax({
                        type: "POST",
                        data: ({email: input_email}),
                        dataType: "json",
                        url: "<?=$baseUrl?>/visitors/deny",

                        success: function(data){
                            if(data.result){
                                $('#status-permission-'+id).remove();
                                $('#comment-menu-'+id).remove();
                                $('#permission-user-'+id).empty().append('Succesfully removing user permission');
                            }else if(data.error){
                                alert('No comment selected');
                            }else{
                                alert('Please check your connection').show();
                            }
                        },
                        error:function(XMLHttpRequest,textStatus, errorThrown)
                        {
                            var status = XMLHttpRequest.status;
                            if(status == '403'){
                                alert('Please check your connection').show();
                            }
                        }
                    });
            }else{
                alert('No comment selected');
            }
            return false;
        });

        $('.comment-block-title a.spam').click(function(){
            var id = $(this).attr('id').split('-')[1];
           var input_email = $('#email-'+id).text();
            if(input_email.length>0){
                 $.ajax({
                        type: "POST",
                        data: ({email: input_email}),
                        dataType: "json",
                        url: "<?=$baseUrl?>/visitors/spam",

                        success: function(data){
                            if(data.result){
                                $('#comment-block-'+id).fadeOut('slow',function(){
                                    $(this).remove();
                                });
                            }else if(data.error){
                                alert('No comment selected');
                            }else{
                                alert('Please check your connection').show();
                            }
                        },
                        error:function(XMLHttpRequest,textStatus, errorThrown)
                        {
                            var status = XMLHttpRequest.status;
                            if(status == '403'){
                                alert('Please check your connection').show();
                            }
                        }
                    });
            }else{
                alert('No comment selected');
            }
            return false;
        });
    });
</script>

<div id="admin-wrapper">
    <div id="admin-comment-title">
        <h1>Managing comments</h1>
    </div>
    <div id="admin-comment">
        <?php if(!empty($comments)) :?>
        <?php foreach($comments as $comment): ?>
        <div class="comment-block" id="comment-block-<?=$comment['Comment']['comment_id']?>">
            <div class="comment-block-title">
                <?php if(!empty($comment['Comment']['website'])):?>
                    <strong> <?=$html->link($comment['Comment']['name'],'#')?></strong> (<span id="email-<?=$comment['Comment']['comment_id']?>"><?=$comment['Comment']['email']?></span>) |
                <?php else:?>
                    <strong><?=$comment['Comment']['name']?></strong> |
                <?php endif;?>
                <?=date("d M Y H:i a", strtotime($comment['Comment']['created']));?> |
                <?php if($comment['Comment']['status'] == '0') :?>
                    <span id="comment-menu-<?=$comment['Comment']['comment_id']?>">
                        <?=$html->link('approve','#',array('title'=>'Approve this comment','class'=>'approve','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?> |
                        <?=$html->link('delete','#',array('title'=>'Deleting this comment','class'=>'delete','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?> |
                        <?=$html->link('give user comment permission','#',array('title'=>'This user will be approved directly by email name','class'=>'permission','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?> |
                        <?=$html->link('block this user','#',array('title'=>'Block this user comments foverer','class'=>'spam','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?>
                    </span>
                    <span id="permission-result-<?=$comment['Comment']['comment_id']?>"></span>
                <?php else:?>
                    <?php if($comment['Comment']['visitor_id'] == '0') :?>
                        <span id="permission-user-<?=$comment['Comment']['comment_id']?>"><?=$html->link('give user comment permission','#',array('title'=>'This user will be approved directly by email name','class'=>'permission','id'=>'statuspermission-'.$comment['Comment']['comment_id']))?></span> |
                    <?php else: ?>
                         <span id="permission-user-<?=$comment['Comment']['comment_id']?>"><?=$html->link('remove user comment permission','#',array('title'=>'Delete this user permission without deleting comments','class'=>'deny','id'=>'statuspermission-'.$comment['Comment']['comment_id']))?></span> |
                    <?php endif; ?>
                    <?=$html->link('block this user','#',array('title'=>'Block this user comments foverer','class'=>'spam','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?> |
                    <?=$html->link('delete','#',array('title'=>'Deleting this comment','class'=>'delete','id'=>'statuscomment-'.$comment['Comment']['comment_id']))?>
                <?php endif;?>
                 <span id="comment-status-<?=$comment['Comment']['comment_id']?>" style="display:none;"></span>
                <br />
                commented on <strong><?=$html->link($comment['Post']['title'],$comment['Post']['slug'])?></strong>                
            </div>
            <div class="comment-block-description">
                <?=$comment['Comment']['description']?>
            </div>
            <div class="clear"></div>
        </div>
        <?php endforeach; ?>
        <?php endif;?>
    </div>

    <div id="pagination">
        <?php $paginator->options(array('url' => $this->passedArgs,'separate'=>''));?>
        <?php
        echo $paginator->prev('&laquo; Prev ', array('escape' => false,'id'=>'prev'));
        echo '&nbsp';
        echo $paginator->numbers(array('class' => 'selectedm','separator'=>'&nbsp;'));
        echo '&nbsp';
        echo $paginator->next(' Next &raquo;', array('escape' => false,'id'=>'next'));
        ?>
    </div>
</div>